/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vladimirkolarevic.eventplanner.hibernate;

import com.vladimirkolarevic.eventplanner.Event;
import com.vladimirkolarevic.eventplanner.User;
import com.vladimirkolarevic.eventplanner.beans.UserBean;
import com.vladimirkolarevic.eventplanner.service.MembershipsService;
import java.io.Serializable;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;

/**
 * 
 * @author Vlada
 */
public class MembershipsServiceHibernate implements MembershipsService {

	private final Session session;

	public MembershipsServiceHibernate(Session session) {
		this.session = session;
	}
	
	
	
	public Long addUser(User user) {

		Long id;
		id = (Long) session.save(user);
		return id;

	}


	public void removeUser(User user) {
		throw new UnsupportedOperationException("Not supported yet.");
	}
	
	
	
	public boolean changeUserInfo(User user) {
			session.update(user);
			return true;
		
	}


	public User findUser(Serializable id) {
		return (User) session.load(UserBean.class, id);
	}
	
	
	public User getUserByUserName(Serializable username) {
		return (User) session.createQuery("from UserBean where username = :u")
				.setString("u", (String) username).uniqueResult();
	}

	
	public User getUser(User user) {
		Example ue = Example.create(user).excludeProperty("id")
		.excludeProperty("firstName")
		.excludeProperty("secondName")
		.excludeProperty("email")
		.excludeProperty("events")
		.ignoreCase().enableLike(MatchMode.ANYWHERE);
		Criteria crit = session.createCriteria(UserBean.class).add(ue);
		return (User) crit.uniqueResult();
		/*User u = getUserByUserName(user.getUserName());
		if (u != null && user.getPassword().equals(u.getPassword())){
			
			return u;
			
		}else{
			
			return null;
		}*/
	}



	public List<Event> getEvents(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

}
